import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:landlore/customWidget/roomManageCard.dart';
import 'package:landlore/dbUtil/databaseHelper.dart';
import 'package:landlore/model/roomInfo.dart';
import 'package:landlore/screens/roomEditScreen.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RoomManageScreen extends StatefulWidget {
  @override
  State<RoomManageScreen> createState() => _RoomManageScreenState();
}

class _RoomManageScreenState extends State<RoomManageScreen> {
  List roomList = [];

  @override
  initState() {
    super.initState();
    _getRoomList();
  }

  _getRoomList() async {
    DatabaseHelper db = DatabaseHelper.instance;
    List<RoomInfo> result = await db.getRoomInfoAll();
    setState(() {
      roomList = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      ///设置文字大小不随系统设置改变
      data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: const Color(0xffe9c46a),
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back_ios,
              color: Colors.black,
              size: 35.sp,
            ),
            onPressed: () => Navigator.pop(context),
          ),
          title: Text("房屋管理",
              style: TextStyle(
                  decoration: TextDecoration.none,
                  fontSize: 35.sp,
                  color: Colors.black,
                  fontWeight: FontWeight.w900)),
          actions: [
            IconButton(
                icon: Icon(
                  Icons.add_business_rounded,
                  color: Colors.black,
                  size: 35.sp,
                ),
                onPressed: () async {
                  await Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) =>
                              RoomEditScreen(roomInfo: RoomInfo.empty())
                      )
                  );
                  _getRoomList();
                })
          ],
          centerTitle: true,
        ),
        body: Container(
            decoration:  BoxDecoration(
                color: const Color(0xfff4a261),
                image: DecorationImage(
                    image: const AssetImage('assets/images/crossword.png'),
                    repeat: ImageRepeat.repeat,
                    colorFilter: ColorFilter.mode(Colors.black.withOpacity(0.3), BlendMode.srcIn)
                )),
            child: roomList.isEmpty
                ? Center(
              child: Text(
                '还没有可用房源',
                style: TextStyle(
                    fontFamily: '欢字卡通手书',
                    fontSize: 55.sp,
                    color: Colors.black),
              ),
            )
                : ListView.builder(
                itemCount: roomList.length,
                itemBuilder: (context, index) {
                  return RoomManageCard(
                    roomInfo: roomList[index],
                    callback: (newState) {
                      setState(() {
                        roomList[index].state = newState ? 1 : 0;
                      });
                    },
                    popCallback: () {
                      //返回刷新数据
                      _getRoomList();
                    },
                  );
                })),
      ),
    );
  }
}
